package com.zh.chat.gui.tree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import com.zh.chat.entity.Friend;
import com.zh.chat.util.Commons;
import com.zh.chat.util.FileOperationUtil;
import com.zh.chat.util.FileType;

public class IconNodeRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = -3494723715969483744L;

	// 下面这个方法重写请自己查看API各个参数的意思
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
				row, hasFocus);
		setForeground(new Color(50, 150, 190));
		setFont(new Font("新宋体", Font.PLAIN, 13));
		setBackground(Color.WHITE);
		// IconTreeNode是重写的继承DefaultMutableTreeNode的带图片的节点
		if(value instanceof IconTreeNode){
			IconTreeNode node = (IconTreeNode) value;
			Friend friend = node.getFriend();
			if(friend!=null){
				String path = FileOperationUtil.getFilePath(FileType.SCULPTURE, friend.getSysId(), friend.getImage());
				setLayout(null);
				setText("<html>"+friend.getFriendId()+"<br>"+friend.getNickname()+"（"+Commons.parseStatus(friend.getStatus())+"）"+"</html>");
				BufferedImage image = null;
				try {
					image = ImageIO.read(new File(path));
				} catch (IOException e) {}
				Image img = new BufferedImage(40, 40, BufferedImage.TRANSLUCENT);
				Graphics g = img.getGraphics();
				g.setColor(Color.LIGHT_GRAY);
				g.fillRect(0, 0, 40, 40);
				g.drawImage(image, 1, 1, 38, 38, null);
				setIcon(new ImageIcon(img));
				setBackground(Color.lightGray);
			}else{
				setText(node.getText());
				URL url = null;
				if(expanded){
					url = IconNodeRenderer.class.getClassLoader().getResource("com/zh/chat/image/expanded.png");
				}else{
					url = IconNodeRenderer.class.getClassLoader().getResource("com/zh/chat/image/unexpanded.png");
				}
				try {
					BufferedImage image = ImageIO.read(url);
					Image img = new BufferedImage(15, 15, BufferedImage.TRANSLUCENT);
					Graphics g = img.getGraphics();
					g.drawImage(image, 0, 0, 15, 15, null);
					ImageIcon icon = new ImageIcon(img);
					setIcon(icon);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			this.setPreferredSize(new Dimension(200, 40));
		}
		return this;
	}// end of getTreeCellRendererComponent
	@Override
	public void setBackground(Color color) {
		super.setBackground(Color.WHITE);
	}
	@Override
	public void setBackgroundSelectionColor(Color newColor) {
		super.setBackgroundSelectionColor(Color.WHITE);
	}
	@Override
	public void setOpaque(boolean isOpaque) {
		super.setOpaque(false);
	}
}